﻿Imports System.Globalization
Imports System.IO

Partial Public Class Daily_Report
    Inherits System.Web.UI.Page

    Dim clsget As New ClassFunctionGetData
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not (IsPostBack) Then
            Dim lbPage As Label = CType(Master.FindControl("lbPage"), Label)
            lbPage.Text = "PTOL LOADING / DISCHARGING DAILY REPORT"
        End If
    End Sub

    Private Sub MainPage_PreInit(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreInit
        Dim ds As DataSet = Session("UserData")
        If Not (ds Is Nothing) Then
            Page.MasterPageFile = clsGet.GetMaster_Page(ds.Tables(0).Rows(0).Item("User_Name").ToString(), "M")
        End If
    End Sub

    Private Function LoadValue(ByVal _Date As String, ByVal Load As String) As DataTable
        Dim D1 As String = CDate(_Date).AddYears(543).ToString("yyyyMMdd", New CultureInfo("en-US"))
        Dim D2 As String = CDate(_Date).AddYears(543).AddDays(-1).ToString("yyyyMMdd", New CultureInfo("en-US"))
        Dim Time As String = ""
        Try
            Time = txtTIme.Text.Split(":")(0) & txtTIme.Text.Split(":")(1)
        Catch ex As Exception

        End Try

        Dim dt As DataTable = clsget.Get_For_DailyReport(D1, D2, Time)
        Dim dtDaily As DataTable = CreateDS()
        Dim wharf As String = ""
        Dim Ship As String = ""
        Dim Berth As String = ""
        Dim Depart As String = ""
        Dim third As String = ""
        Dim C As Integer = ViewState("C_")
        Dim W1 As Boolean = True
        Dim W2 As Boolean = True
        Dim W3 As Boolean = True
        Dim W4 As Boolean = True
        Dim W5 As Boolean = True
        Dim W6 As Boolean = True

        For Each dm As DataRow In dt.Rows

            wharf = dm.Item("Wharf")

            Ship = dm.Item("Vehicle_Text")
            Dim B As String = dm.Item("Berthing_Date")
            Try
                B = CDate(B).ToString("dd/MM/") & B.Split(" ")(2).Substring(2, 2)
            Catch ex As Exception

            End Try
            Berth = B & " @ " & dm.Item("Berthing_TimeTo")
            Depart = dm.Item("Depart_Actual_Date") & " @ " & dm.Item("Depart_Actual_Time")
            Dim dtProduct As DataTable = clsget.Get_Load_Unload_Vessel(dm.Item("Booking_no"))

            If (dtProduct.Rows.Count > 0) Then
                third = dtProduct.Rows(0).Item("Third_Party")
            End If
            dtProduct = clsget.Get_Booking_ProductOperation(dm.Item("Booking_no"))
            If (dtProduct.Rows.Count > 0) Then
                If (dtProduct.Rows(0).Item("Product_Event") = Load) Then
                    Select Case (dm.Item("Wharf"))
                        Case "1"
                            W1 = False
                        Case "2A"
                            W2 = False
                        Case "2B"
                            W3 = False
                        Case "3"
                            W4 = False
                        Case "4"
                            W5 = False
                        Case "5"
                            W6 = False
                    End Select
                    Dim First As Boolean = True
                    For Each dp As DataRow In dtProduct.Rows
                        Try
                            Dim dr As DataRow = dtDaily.NewRow()
                            dr.Item("Wharf") = wharf
                            dr.Item("Booking_no") = dp.Item("Booking_no") & ""
                            dr.Item("Product_no") = dp.Item("Product_no") & ""
                            If (First) Then
                                dr.Item("Ship") = Ship
                                dr.Item("Berth") = Berth
                                dr.Item("Depart") = Depart
                                If (Depart = "") Then
                                    dr.Item("Status") = "DURING LOADING"
                                Else
                                    dr.Item("Status") = "COMPLETED"
                                End If

                                Dim dtLog_in As DataTable = clsget.Get_Booking_LogSheet_In(dm.Item("Booking_no"))
                                Dim dtLoad_Vessel As DataTable = clsget.Get_Load_Unload_Vessel(dm.Item("Booking_no"))
                                Try
                                    Dim ts = DateDiff("n", dtLog_in.Rows(0).Item("Berthing_Date") & " " & dtLog_in.Rows(0).Item("Berthing_TimeTo"), dtLoad_Vessel.Rows(0).Item("Depart_Standard_Date").ToString.Substring(0, 2) & " " & GetNameMonth(dtLoad_Vessel.Rows(0).Item("Depart_Standard_Date").ToString.Substring(3, 2)) & " " & "20" & dtLoad_Vessel.Rows(0).Item("Depart_Standard_Date").ToString.Substring(6, 2) & " " & dtLoad_Vessel.Rows(0).Item("Depart_Standard_Time"))
                                    Dim Hour As String = ts \ 60
                                    Dim Minute As String = ts Mod 60

                                    dr.Item("STD") = CInt(Hour) & ":" & CInt(Minute).ToString("00")
                                Catch ex As Exception

                                End Try
                                Try
                                    Dim ts = DateDiff("n", dtLog_in.Rows(0).Item("Berthing_Date") & " " & dtLog_in.Rows(0).Item("Berthing_TimeTo"), dtLoad_Vessel.Rows(0).Item("Depart_Actual_Date").ToString.Substring(0, 2) & " " & GetNameMonth(dtLoad_Vessel.Rows(0).Item("Depart_Actual_Date").ToString.Substring(3, 2)) & " " & "20" & dtLoad_Vessel.Rows(0).Item("Depart_Actual_Date").ToString.Substring(6, 2) & " " & dtLoad_Vessel.Rows(0).Item("Depart_Actual_Time"))

                                    Dim Hour As String = ts \ 60
                                    Dim Minute As String = ts Mod 60

                                    dr.Item("Total") = CInt(Hour) & ":" & CInt(Minute).ToString("00")
                                Catch ex As Exception

                                End Try

                                Try
                                    Dim ts = DateDiff("n", dtLog_in.Rows(0).Item("Berthing_Date") & " " & dtLog_in.Rows(0).Item("Berthing_TimeTo"), dtLoad_Vessel.Rows(0).Item("Depart_Actual_Date").ToString.Substring(0, 2) & " " & GetNameMonth(dtLoad_Vessel.Rows(0).Item("Depart_Actual_Date").ToString.Substring(3, 2)) & " " & "20" & dtLoad_Vessel.Rows(0).Item("Depart_Actual_Date").ToString.Substring(6, 2) & " " & dtLoad_Vessel.Rows(0).Item("Depart_Actual_Time"))
                                    Dim actual = ts
                                    ts = DateDiff("n", dtLog_in.Rows(0).Item("Berthing_Date") & " " & dtLog_in.Rows(0).Item("Berthing_TimeTo"), dtLoad_Vessel.Rows(0).Item("Depart_Standard_Date").ToString.Substring(0, 2) & " " & GetNameMonth(dtLoad_Vessel.Rows(0).Item("Depart_Standard_Date").ToString.Substring(3, 2)) & " " & "20" & dtLoad_Vessel.Rows(0).Item("Depart_Standard_Date").ToString.Substring(6, 2) & " " & dtLoad_Vessel.Rows(0).Item("Depart_Standard_Time"))
                                    If (ts >= actual) Then
                                        ts = ts - actual
                                        Dim Hour As String = ts \ 60
                                        Dim Minute As String = ts Mod 60
                                        dr.Item("DiffTime") = CInt(Hour) & ":" & CInt(Minute).ToString("00")
                                    Else
                                        ts = actual - ts
                                        Dim Hour As String = ts \ 60
                                        Dim Minute As String = ts Mod 60
                                        dr.Item("DiffTime") = "(" & CInt(Hour) & ":" & CInt(Minute).ToString("00") & ") " & C.ToString()
                                        C += 1
                                    End If
                                Catch ex As Exception

                                End Try

                                First = False
                            End If

                            dr.Item("Third") = third


                            dr.Item("Product") = dp.Item("Product_name")
                            Dim dtShore As DataTable = clsget.Get_Shore(dp.Item("Shore_Code"), "")
                            If (dtShore.Rows.Count > 0) Then
                                dr.Item("Shore") = dtShore.Rows(0).Item("Shore_Name") & ""
                            End If
                            dtShore.Dispose()

                            Dim dtWharf As DataTable = clsget.Get_BookingWharf(dm.Item("Booking_no"))
                            Dim dtValue As DataTable = clsget.Get_StandardTimeLiquid(dp.Item("Shore_Code"), dp.Item("Group_no"), dp.Item("Line_Code"), dp.Item("Product_Event"), dtWharf.Rows(0).Item("BookingWharf") & "", dp.Item("Facility_Code"))
                            Dim UN As String = ""
                            If (dtValue.Rows.Count > 0) Then
                                UN = dtValue.Rows(0).Item("Unit") & ""
                                dr.Item("Unit") = dtValue.Rows(0).Item("Unit") & ""
                                dr.Item("Standard") = dtValue.Rows(0).Item("Rate") & ""
                                dr.Item("UnitFlow") = dtValue.Rows(0).Item("Unit") & ""
                            Else
                                dr.Item("Unit") = ""
                                dr.Item("Standard") = ""
                                dr.Item("UnitFlow") = ""
                            End If

                            dtValue = clsget.Get_Load_Unload_Vessel(dm.Item("Booking_no"))
                            If (dtValue.Rows.Count > 0) Then
                                dr.Item("Third") = dtValue.Rows(0).Item("Third_Party")
                            End If
                            dtValue = clsget.Get_Load_Unload_Product(dm.Item("Booking_no"), dp.Item("Product_no"))
                            If (dtValue.Rows.Count > 0) Then
                                If (UN = "M3") Then
                                    dr.Item("BL") = dtValue.Rows(0).Item("LU_BL") & ""
                                    dr.Item("SL") = dtValue.Rows(0).Item("LU_SL") & ""
                                ElseIf (UN = "MT") Then
                                    dr.Item("BL") = dtValue.Rows(0).Item("LU_BLMT") & ""
                                    dr.Item("SL") = dtValue.Rows(0).Item("LU_SLMT") & ""
                                End If
                                If (Load = "L") Then
                                    Try
                                        If (CDbl(dr.Item("SL")) >= CDbl(dr.Item("BL"))) Then
                                            dr.Item("Diff") = FormatNumber((CDbl(dr.Item("SL")) - CDbl(dr.Item("BL"))), 3)
                                        Else
                                            dr.Item("Diff") = "(" & FormatNumber((CDbl(dr.Item("BL")) - CDbl(dr.Item("SL"))), 3) & ")"
                                        End If
                                        If (CDbl(dr.Item("SL")) >= CDbl(dr.Item("BL"))) Then
                                            dr.Item("PersendDiff") = FormatNumber(((CDbl(dr.Item("SL")) - CDbl(dr.Item("BL"))) / dr.Item("BL")) * 100, 2) & "%"
                                        Else
                                            dr.Item("PersendDiff") = "(" & FormatNumber(((CDbl(dr.Item("BL")) - CDbl(dr.Item("SL"))) / dr.Item("BL")) * 100, 2) & ")%"
                                        End If
                                        dr.Item("BL") = FormatNumber(CDbl(dr.Item("BL")))
                                        dr.Item("SL") = FormatNumber(CDbl(dr.Item("SL")))


                                    Catch ex As Exception

                                    End Try


                                    dr.Item("E_API") = dtValue.Rows(0).Item("API_Shore") & ""
                                    dr.Item("E_VIS") = dtValue.Rows(0).Item("VIS_Shore") & ""
                                    dr.Item("S_API") = dtValue.Rows(0).Item("API_Ship") & ""
                                    dr.Item("S_VIS") = dtValue.Rows(0).Item("VIS_Ship") & ""
                                Else
                                    If (UN = "M3") Then
                                        dr.Item("Diff") = dtValue.Rows(0).Item("LU_SU") & ""
                                    ElseIf (UN = "MT") Then
                                        dr.Item("Diff") = dtValue.Rows(0).Item("LU_SUMT") & ""
                                    End If
                                    '3& 1
                                    Try
                                        If (CDbl(dr.Item("Diff")) >= CDbl(dr.Item("BL"))) Then
                                            dr.Item("E_API") = FormatNumber((CDbl(dr.Item("Diff")) - CDbl(dr.Item("BL"))), 3)
                                        Else
                                            dr.Item("E_API") = "(" & FormatNumber((CDbl(dr.Item("BL")) - CDbl(dr.Item("Diff"))), 3) & ")"
                                        End If
                                    Catch ex As Exception

                                    End Try

                                    Try
                                        If (CDbl(dr.Item("Diff")) >= CDbl(dr.Item("BL"))) Then
                                            dr.Item("E_VIS") = FormatNumber(CDbl(CDbl(dr.Item("Diff")) - CDbl(dr.Item("BL"))) / CDbl(dr.Item("BL")) * 100, 2) & "%"
                                        Else
                                            dr.Item("E_VIS") = "(" & FormatNumber(CDbl(CDbl(dr.Item("BL")) - CDbl(dr.Item("Diff"))) / CDbl(dr.Item("BL")) * 100, 2) & ")%"
                                        End If
                                    Catch ex As Exception

                                    End Try

                                    '3 & 2
                                    Try
                                        If (CDbl(dr.Item("Diff")) >= CDbl(dr.Item("SL"))) Then
                                            dr.Item("S_API") = FormatNumber((CDbl(dr.Item("Diff")) - CDbl(dr.Item("SL"))), 3)
                                        Else
                                            dr.Item("S_API") = "(" & FormatNumber((CDbl(dr.Item("SL")) - CDbl(dr.Item("Diff"))), 3) & ")"
                                        End If
                                    Catch ex As Exception

                                    End Try
                                    Try
                                        If (CDbl(dr.Item("Diff")) >= CDbl(dr.Item("SL"))) Then
                                            dr.Item("S_VIS") = FormatNumber(CDbl(CDbl(dr.Item("Diff")) - CDbl(dr.Item("SL"))) / CDbl(dr.Item("SL")) * 100, 2) & "%"
                                        Else
                                            dr.Item("S_VIS") = "(" & FormatNumber(CDbl(CDbl(dr.Item("SL")) - CDbl(dr.Item("Diff"))) / CDbl(dr.Item("SL")) * 100, 2) & ")%"
                                        End If
                                    Catch ex As Exception

                                    End Try

                                    dr.Item("BL") = FormatNumber(CDbl(dr.Item("BL")))
                                    dr.Item("SL") = FormatNumber(CDbl(dr.Item("SL")))
                                    dr.Item("Diff") = FormatNumber(CDbl(dr.Item("Diff")))
                                End If




                                Dim StartDateActual As String = dtValue.Rows(0).Item("LU_StartActual_Date").ToString.Substring(0, 2) & " " & GetNameMonth(dtValue.Rows(0).Item("LU_StartActual_Date").ToString.Substring(3, 2)) & " " & "20" & dtValue.Rows(0).Item("LU_StartActual_Date").ToString.Substring(6, 2)
                                Dim EndDateActual As String = dtValue.Rows(0).Item("LU_StopActual_Date").ToString.Substring(0, 2) & " " & GetNameMonth(dtValue.Rows(0).Item("LU_StopActual_Date").ToString.Substring(3, 2)) & " " & "20" & dtValue.Rows(0).Item("LU_StopActual_Date").ToString.Substring(6, 2)
                                Dim ts = DateDiff("n", StartDateActual & " " & dtValue.Rows(0).Item("LU_StartActual_Time"), EndDateActual & " " & dtValue.Rows(0).Item("LU_StopActual_Time"))
                                'ทำการลบค่า Delay ออก
                                Dim dtDelay As DataTable = clsget.Get_Load_Unload_Delay(dm.Item("Booking_no"))
                                If Not (dtDelay Is Nothing) Then
                                    For Each dd As DataRow In dtDelay.Rows
                                        Try
                                            Dim startDate As String = dd.Item("Date_From").ToString.Substring(0, 2) & " " & GetNameMonth(dd.Item("Date_From").ToString.Substring(3, 2)) & " " & "20" & dd.Item("Date_From").ToString.Substring(6, 2)
                                            Dim EndDate As String = dd.Item("Date_To").ToString.Substring(0, 2) & " " & GetNameMonth(dd.Item("Date_To").ToString.Substring(3, 2)) & " " & "20" & dd.Item("Date_To").ToString.Substring(6, 2)

                                            If (CDate(startDate) = CDate(StartDateActual)) Then
                                                'วันเดียวกัน
                                                If (CInt(dd.Item("Time_From").ToString().Split(":")(0)) = CInt(dr.Item("LU_StartActual_Time").ToString().Split(":")(0))) Then
                                                    If (CInt(dd.Item("Time_From").ToString().Split(":")(1)) >= CInt(dr.Item("LU_StartActual_Time").ToString().Split(":")(1))) Then
                                                        If (CInt(dd.Item("Time_From").ToString().Split(":")(0)) < CInt(dr.Item("LU_StopActual_Time").ToString().Split(":")(0))) Then
                                                            Dim ts2 = DateDiff("n", startDate & " " & dd.Item("Time_From"), EndDate & " " & dd.Item("Time_To"))
                                                            ts -= ts2
                                                        ElseIf (CInt(dd.Item("Time_From").ToString().Split(":")(0)) = CInt(dr.Item("LU_StopActual_Time").ToString().Split(":")(0))) Then
                                                            If (CInt(dd.Item("Time_From").ToString().Split(":")(1)) <= CInt(dr.Item("LU_StopActual_Time").ToString().Split(":")(1))) Then
                                                                Dim ts2 = DateDiff("n", startDate & " " & dd.Item("Time_From"), EndDate & " " & dd.Item("Time_To"))
                                                                ts -= ts2
                                                            End If
                                                        End If
                                                    End If
                                                ElseIf (CInt(dd.Item("Time_From").ToString().Split(":")(0)) > CInt(dr.Item("LU_StartActual_Time").ToString().Split(":")(0))) Then
                                                    If (CInt(dd.Item("Time_From").ToString().Split(":")(0)) < CInt(dr.Item("LU_StopActual_Time").ToString().Split(":")(0))) Then
                                                        Dim ts2 = DateDiff("n", startDate & " " & dd.Item("Time_From"), EndDate & " " & dd.Item("Time_To"))
                                                        ts -= ts2
                                                    ElseIf (CInt(dd.Item("Time_From").ToString().Split(":")(0)) = CInt(dr.Item("LU_StopActual_Time").ToString().Split(":")(0))) Then
                                                        If (CInt(dd.Item("Time_From").ToString().Split(":")(1)) <= CInt(dr.Item("LU_StopActual_Time").ToString().Split(":")(1))) Then
                                                            Dim ts2 = DateDiff("n", startDate & " " & dd.Item("Time_From"), EndDate & " " & dd.Item("Time_To"))
                                                            ts -= ts2
                                                        End If
                                                    End If
                                                End If
                                            ElseIf (CDate(startDate) > CDate(StartDateActual)) Then
                                                If (CDate(startDate) < CDate(EndDateActual)) Then
                                                    'ตกในช่วง
                                                    Dim ts2 = DateDiff("n", startDate & " " & dd.Item("Time_From"), EndDate & " " & dd.Item("Time_To"))
                                                    ts -= ts2
                                                ElseIf (CDate(startDate) = CDate(EndDateActual)) Then
                                                    'ตกในช่วง
                                                    If (CInt(dd.Item("Time_From").ToString().Split(":")(0)) < CInt(dr.Item("LU_StopActual_Time").ToString().Split(":")(0))) Then
                                                        Dim ts2 = DateDiff("n", startDate & " " & dd.Item("Time_From"), EndDate & " " & dd.Item("Time_To"))
                                                        ts -= ts2
                                                    ElseIf (CInt(dd.Item("Time_From").ToString().Split(":")(0)) = CInt(dr.Item("LU_StopActual_Time").ToString().Split(":")(0))) Then
                                                        If (CInt(dd.Item("Time_From").ToString().Split(":")(1)) <= CInt(dr.Item("LU_StopActual_Time").ToString().Split(":")(1))) Then
                                                            Dim ts2 = DateDiff("n", startDate & " " & dd.Item("Time_From"), EndDate & " " & dd.Item("Time_To"))
                                                            ts -= ts2
                                                        End If
                                                    End If
                                                End If

                                            End If
                                        Catch ex As Exception

                                        End Try

                                    Next
                                End If
                                Try
                                    dr.Item("Actual") = CInt(CDbl(dr.Item("BL")) / (CDbl(ts) / 60)).ToString("00")
                                Catch ex As Exception

                                End Try
                                Try
                                    If (dr.Item("Standard") <= dr.Item("Actual")) Then
                                        dr.Item("DiffFlow") = CInt(dr.Item("Actual")) - CInt(dr.Item("Standard"))
                                    Else
                                        dr.Item("DiffFlow") = "(" & CInt(dr.Item("Standard")) - CInt(dr.Item("Actual")) & ")"
                                    End If
                                Catch ex As Exception

                                End Try

                            End If

                            dtDaily.Rows.Add(dr)
                            dtDaily.AcceptChanges()
                        Catch ex As Exception

                        End Try

                    Next

                End If
            End If

        Next
        If (W1) Then
            Dim dr As DataRow = dtDaily.NewRow
            dr.Item("Wharf") = "1"
            dtDaily.Rows.Add(dr)
            dtDaily.AcceptChanges()
        End If
        If (W2) Then
            Dim dr As DataRow = dtDaily.NewRow
            dr.Item("Wharf") = "2A"
            dtDaily.Rows.Add(dr)
            dtDaily.AcceptChanges()
        End If
        If (W3) Then
            Dim dr As DataRow = dtDaily.NewRow
            dr.Item("Wharf") = "2B"
            dtDaily.Rows.Add(dr)
            dtDaily.AcceptChanges()
        End If
        If (W4) Then
            Dim dr As DataRow = dtDaily.NewRow
            dr.Item("Wharf") = "3"
            dtDaily.Rows.Add(dr)
            dtDaily.AcceptChanges()
        End If
        If (W5) Then
            Dim dr As DataRow = dtDaily.NewRow
            dr.Item("Wharf") = "4"
            dtDaily.Rows.Add(dr)
            dtDaily.AcceptChanges()
        End If
        If (W6) Then
            Dim dr As DataRow = dtDaily.NewRow
            dr.Item("Wharf") = "5"
            dtDaily.Rows.Add(dr)
            dtDaily.AcceptChanges()
        End If
        ViewState("C_") = C
        Dim dsView As DataView = dtDaily.DefaultView
        dsView.Sort = " Wharf "
        dtDaily = New DataTable
        dtDaily = clsget.ConvertDataViewToDataSet(dsView).Copy
        Return dtDaily
    End Function

    Private Function CreateDS() As DataTable
        Dim dt As New DataTable
        dt.Columns.Add("Wharf", GetType(String))
        dt.Columns.Add("Ship", GetType(String))
        dt.Columns.Add("Product", GetType(String))
        dt.Columns.Add("Shore", GetType(String))
        dt.Columns.Add("Third", GetType(String))
        dt.Columns.Add("Unit", GetType(String))
        dt.Columns.Add("BL", GetType(String))
        dt.Columns.Add("SL", GetType(String))
        dt.Columns.Add("Diff", GetType(String))
        dt.Columns.Add("PersendDiff", GetType(String))
        dt.Columns.Add("E_API", GetType(String))
        dt.Columns.Add("E_VIS", GetType(String))
        dt.Columns.Add("S_API", GetType(String))
        dt.Columns.Add("S_VIS", GetType(String))
        dt.Columns.Add("Accept", GetType(String))
        dt.Columns.Add("Standard", GetType(String))
        dt.Columns.Add("UnitFlow", GetType(String))
        dt.Columns.Add("Actual", GetType(String))
        dt.Columns.Add("DiffFlow", GetType(String))
        dt.Columns.Add("Berth", GetType(String))
        dt.Columns.Add("Depart", GetType(String))
        dt.Columns.Add("STD", GetType(String))
        dt.Columns.Add("Total", GetType(String))
        dt.Columns.Add("DiffTime", GetType(String))
        dt.Columns.Add("Demurrage", GetType(String))
        dt.Columns.Add("Pig", GetType(String))
        dt.Columns.Add("FW", GetType(String))
        dt.Columns.Add("Bunker", GetType(String))
        dt.Columns.Add("Status", GetType(String))
        dt.Columns.Add("Head", GetType(String))
        dt.Columns.Add("Booking_no", GetType(String))
        dt.Columns.Add("Product_no", GetType(String))
        dt.AcceptChanges()
        Return dt
    End Function

    Private Function GetRate(ByVal dtproduct As DataTable, ByVal Shore As String, ByVal Group_no As String, ByVal Line As String, ByVal LD As String, ByVal BookingWharf As String, ByVal Facility As String) As String
        Dim dt As DataTable = clsget.Get_StandardTimeLiquid(Shore, Group_no, Line, LD, BookingWharf, Facility)
        ' dt.Rows(0).Item("Rate")
        If (dt.Rows.Count > 0) Then
            Dim LPG As Boolean = False
            Dim BUTANE As Boolean = False
            Dim LPG_BUTANE As Boolean = Facility
            For Each dr As DataRow In dtproduct.Rows
                If (dr.Item("Group_no") = "00031") Or (dr.Item("Group_no") = "00064") Then
                    LPG = True
                End If
                If (dr.Item("Group_no") = "00034") Then
                    BUTANE = True
                End If
            Next
            If (LPG) And (BUTANE) Then
                dt = clsget.Get_StandardTimeLiquid(Shore, "00033", Line, LD, BookingWharf, Facility)
            End If
            Return dt.Rows(0).Item("Rate") & ""
        Else
            Return 0
        End If
    End Function

    Private Sub gridItem_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gridItem.RowCreated
        If e.Row.RowType = DataControlRowType.Header Then
            Dim Grid As GridView = CType(sender, GridView)
            e.Row.Cells.Clear()
            Dim GridRow As GridViewRow = New GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Insert)
            Dim GridRow1 As GridViewRow = New GridViewRow(1, 0, DataControlRowType.Header, DataControlRowState.Insert)
            Dim GridRow2 As GridViewRow = New GridViewRow(2, 0, DataControlRowType.Header, DataControlRowState.Insert)

            Dim table As TableCell = New TableCell()
            table.Text = "WHARF NO."
            table.HorizontalAlign = HorizontalAlign.Center
            table.RowSpan = 3
            table.CssClass = "locked"
            table.Wrap = True
            GridRow2.Cells.Add(table)

            table = New TableCell()
            table.Text = "Ship Name"
            table.HorizontalAlign = HorizontalAlign.Center
            table.RowSpan = 3
            table.CssClass = "locked"
            table.Wrap = True
            GridRow2.Cells.Add(table)

            table = New TableCell()
            table.Text = "Product"
            table.CssClass = "locked"
            table.HorizontalAlign = HorizontalAlign.Center
            table.RowSpan = 3
            table.Wrap = True
            GridRow2.Cells.Add(table)

            table = New TableCell()
            table.Text = "Shore Tank"
            table.HorizontalAlign = HorizontalAlign.Center
            table.RowSpan = 3
            table.Wrap = True
            GridRow2.Cells.Add(table)

            table = New TableCell()
            table.Text = "Surveyor"
            table.HorizontalAlign = HorizontalAlign.Center
            table.RowSpan = 3
            table.Wrap = True
            GridRow2.Cells.Add(table)

            table = New TableCell()
            table.Text = "Quantity Control"
            table.HorizontalAlign = HorizontalAlign.Center
            table.ColumnSpan = 5
            table.Wrap = True
            GridRow2.Cells.Add(table)

            table = New TableCell()
            table.Text = "Quality Control"
            table.HorizontalAlign = HorizontalAlign.Center
            table.ColumnSpan = 5
            table.Wrap = True
            GridRow2.Cells.Add(table)

            table = New TableCell()
            table.Text = "Flow rate Control"
            table.HorizontalAlign = HorizontalAlign.Center
            table.ColumnSpan = 4
            table.Wrap = True
            GridRow2.Cells.Add(table)

            table = New TableCell()
            table.Text = "Time Control"
            table.HorizontalAlign = HorizontalAlign.Center
            table.ColumnSpan = 6
            table.Wrap = True
            GridRow2.Cells.Add(table)

            table = New TableCell()
            table.Text = "Pig or Flush"
            table.HorizontalAlign = HorizontalAlign.Center
            table.RowSpan = 3
            table.Wrap = True
            GridRow2.Cells.Add(table)

            table = New TableCell()
            table.Text = "FW(MT)"
            table.HorizontalAlign = HorizontalAlign.Center
            table.RowSpan = 3
            table.Wrap = True
            GridRow2.Cells.Add(table)

            table = New TableCell()
            table.Text = "Trucks Bunker"
            table.HorizontalAlign = HorizontalAlign.Center
            table.RowSpan = 3
            table.Wrap = True
            GridRow2.Cells.Add(table)

            table = New TableCell()
            table.Text = "Status"
            table.HorizontalAlign = HorizontalAlign.Center
            table.RowSpan = 3
            table.Wrap = True
            GridRow2.Cells.Add(table)

            Grid.Controls(0).Controls.AddAt(0, GridRow2)

            table = New TableCell()
            table.Text = "Unit"
            table.HorizontalAlign = HorizontalAlign.Center
            table.Wrap = True
            table.RowSpan = 2
            GridRow1.Cells.Add(table)

            table = New TableCell()
            table.Text = "Shore/Meter B/L(1)"
            table.HorizontalAlign = HorizontalAlign.Center
            table.Wrap = True
            table.RowSpan = 2
            GridRow1.Cells.Add(table)

            table = New TableCell()
            table.Text = "Ship Loading(2)"
            table.HorizontalAlign = HorizontalAlign.Center
            table.RowSpan = 2
            table.Wrap = True
            GridRow1.Cells.Add(table)

            table = New TableCell()
            table.Text = "Quan. Diff(1&2)"
            table.HorizontalAlign = HorizontalAlign.Center
            table.RowSpan = 2
            table.Wrap = True
            GridRow1.Cells.Add(table)

            table = New TableCell()
            table.Text = "Percent Diff(%)"
            table.HorizontalAlign = HorizontalAlign.Center
            table.RowSpan = 2
            table.Wrap = True
            GridRow1.Cells.Add(table)

            table = New TableCell()
            table.Text = "Expect"
            table.HorizontalAlign = HorizontalAlign.Center
            table.ColumnSpan = 2
            table.Wrap = True
            GridRow1.Cells.Add(table)

            table = New TableCell()
            table.Text = "Ship"
            table.HorizontalAlign = HorizontalAlign.Center
            table.ColumnSpan = 2
            table.Wrap = True
            GridRow1.Cells.Add(table)

            table = New TableCell()
            table.Text = "Acceptance(Y/N)"
            table.HorizontalAlign = HorizontalAlign.Center
            table.RowSpan = 2
            table.Wrap = True
            GridRow1.Cells.Add(table)

            table = New TableCell()
            table.Text = "Standard rate(/Hr)"
            table.HorizontalAlign = HorizontalAlign.Center
            table.RowSpan = 2
            table.Wrap = True
            GridRow1.Cells.Add(table)

            table = New TableCell()
            table.Text = "Unit"
            table.HorizontalAlign = HorizontalAlign.Center
            table.RowSpan = 2
            table.Wrap = True
            GridRow1.Cells.Add(table)

            table = New TableCell()
            table.Text = "Actual rate(/Hr)"
            table.HorizontalAlign = HorizontalAlign.Center
            table.RowSpan = 2
            table.Wrap = True
            GridRow1.Cells.Add(table)

            table = New TableCell()
            table.Text = "Diff (/Hr)"
            table.HorizontalAlign = HorizontalAlign.Center
            table.RowSpan = 2
            table.Wrap = True
            GridRow1.Cells.Add(table)

            table = New TableCell()
            table.Text = "Berth"
            table.HorizontalAlign = HorizontalAlign.Center
            table.RowSpan = 2
            table.Wrap = True
            GridRow1.Cells.Add(table)

            table = New TableCell()
            table.Text = "Depart"
            table.HorizontalAlign = HorizontalAlign.Center
            table.RowSpan = 2
            table.Wrap = True
            GridRow1.Cells.Add(table)

            table = New TableCell()
            table.Text = "STD. (Hr)"
            table.HorizontalAlign = HorizontalAlign.Center
            table.RowSpan = 2
            table.Wrap = True
            GridRow1.Cells.Add(table)

            table = New TableCell()
            table.Text = "Total (Hr)"
            table.HorizontalAlign = HorizontalAlign.Center
            table.RowSpan = 2
            table.Wrap = True
            GridRow1.Cells.Add(table)

            table = New TableCell()
            table.Text = "Diff (Hr)"
            table.HorizontalAlign = HorizontalAlign.Center
            table.RowSpan = 2
            table.Wrap = True
            GridRow1.Cells.Add(table)

            table = New TableCell()
            table.Text = "Demurrage/(Delay)"
            table.HorizontalAlign = HorizontalAlign.Center
            table.RowSpan = 2
            table.Wrap = True
            GridRow1.Cells.Add(table)

            Grid.Controls(0).Controls.AddAt(1, GridRow1)


            table = New TableCell()
            table.Text = "API"
            table.HorizontalAlign = HorizontalAlign.Center
            table.Wrap = True
            GridRow.Cells.Add(table)

            table = New TableCell()
            table.Text = "VIS"
            table.HorizontalAlign = HorizontalAlign.Center
            table.Wrap = True
            GridRow.Cells.Add(table)

            table = New TableCell()
            table.Text = "API"
            table.HorizontalAlign = HorizontalAlign.Center
            table.Wrap = True
            GridRow.Cells.Add(table)

            table = New TableCell()
            table.Text = "VIS"
            table.HorizontalAlign = HorizontalAlign.Center
            table.Wrap = True
            GridRow.Cells.Add(table)

            Grid.Controls(0).Controls.AddAt(2, GridRow)
        End If
    End Sub

    Private Sub gridDischarge_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gridDischarge.RowCreated
        If e.Row.RowType = DataControlRowType.Header Then
            Dim Grid As GridView = CType(sender, GridView)
            e.Row.Cells.Clear()
            Dim GridRow As GridViewRow = New GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Insert)
            Dim GridRow1 As GridViewRow = New GridViewRow(1, 0, DataControlRowType.Header, DataControlRowState.Insert)
            Dim GridRow2 As GridViewRow = New GridViewRow(2, 0, DataControlRowType.Header, DataControlRowState.Insert)

            Dim table As TableCell = New TableCell()
            table.Text = "WHARF NO."
            table.HorizontalAlign = HorizontalAlign.Center
            table.RowSpan = 3
            table.CssClass = "locked"
            table.Wrap = True
            GridRow2.Cells.Add(table)

            table = New TableCell()
            table.Text = "Ship Name"
            table.HorizontalAlign = HorizontalAlign.Center
            table.RowSpan = 3
            table.CssClass = "locked"
            table.Wrap = True
            GridRow2.Cells.Add(table)

            table = New TableCell()
            table.Text = "Product"
            table.CssClass = "locked"
            table.HorizontalAlign = HorizontalAlign.Center
            table.RowSpan = 3
            table.Wrap = True
            GridRow2.Cells.Add(table)

            table = New TableCell()
            table.Text = "Shore Tank"
            table.HorizontalAlign = HorizontalAlign.Center
            table.RowSpan = 3
            table.Wrap = True
            GridRow2.Cells.Add(table)

            table = New TableCell()
            table.Text = "Surveyor"
            table.HorizontalAlign = HorizontalAlign.Center
            table.RowSpan = 3
            table.Wrap = True
            GridRow2.Cells.Add(table)

            table = New TableCell()
            table.Text = "Quantity Control"
            table.HorizontalAlign = HorizontalAlign.Center
            table.ColumnSpan = 8
            table.Wrap = True
            GridRow2.Cells.Add(table)

            table = New TableCell()
            table.Text = "Quality"
            table.HorizontalAlign = HorizontalAlign.Center
            table.Wrap = True
            GridRow2.Cells.Add(table)

            table = New TableCell()
            table.Text = "Flow rate Control"
            table.HorizontalAlign = HorizontalAlign.Center
            table.ColumnSpan = 4
            table.Wrap = True
            GridRow2.Cells.Add(table)

            table = New TableCell()
            table.Text = "Time Control"
            table.HorizontalAlign = HorizontalAlign.Center
            table.ColumnSpan = 6
            table.Wrap = True
            GridRow2.Cells.Add(table)

            table = New TableCell()
            table.Text = "Pig or Flush"
            table.HorizontalAlign = HorizontalAlign.Center
            table.RowSpan = 3
            table.Wrap = True
            GridRow2.Cells.Add(table)

            table = New TableCell()
            table.Text = "FW(MT)"
            table.HorizontalAlign = HorizontalAlign.Center
            table.RowSpan = 3
            table.Wrap = True
            GridRow2.Cells.Add(table)

            table = New TableCell()
            table.Text = "Trucks Bunker"
            table.HorizontalAlign = HorizontalAlign.Center
            table.RowSpan = 3
            table.Wrap = True
            GridRow2.Cells.Add(table)

            table = New TableCell()
            table.Text = "Status"
            table.HorizontalAlign = HorizontalAlign.Center
            table.RowSpan = 3
            table.Wrap = True
            GridRow2.Cells.Add(table)

            Grid.Controls(0).Controls.AddAt(0, GridRow2)

            table = New TableCell()
            table.Text = "Unit"
            table.HorizontalAlign = HorizontalAlign.Center
            table.Wrap = True
            table.RowSpan = 2
            GridRow1.Cells.Add(table)

            table = New TableCell()
            table.Text = "Loading Port"
            table.HorizontalAlign = HorizontalAlign.Center
            table.Wrap = True
            table.ColumnSpan = 2
            GridRow1.Cells.Add(table)

            table = New TableCell()
            table.Text = "Discharge Port Ship(3)"
            table.HorizontalAlign = HorizontalAlign.Center
            table.RowSpan = 2
            table.Wrap = True
            GridRow1.Cells.Add(table)

            table = New TableCell()
            table.Text = "Diff(B/L)"
            table.HorizontalAlign = HorizontalAlign.Center
            table.ColumnSpan = 2
            table.Wrap = True
            GridRow1.Cells.Add(table)

            table = New TableCell()
            table.Text = "Diff(Intransit)"
            table.HorizontalAlign = HorizontalAlign.Center
            table.ColumnSpan = 2
            table.Wrap = True
            GridRow1.Cells.Add(table)

            table = New TableCell()
            table.Text = "Check and Accept by"
            table.HorizontalAlign = HorizontalAlign.Center
            table.RowSpan = 2
            table.Wrap = True
            GridRow1.Cells.Add(table)

            table = New TableCell()
            table.Text = "Standard rate(/Hr)"
            table.HorizontalAlign = HorizontalAlign.Center
            table.RowSpan = 2
            table.Wrap = True
            GridRow1.Cells.Add(table)

            table = New TableCell()
            table.Text = "Unit"
            table.HorizontalAlign = HorizontalAlign.Center
            table.RowSpan = 2
            table.Wrap = True
            GridRow1.Cells.Add(table)

            table = New TableCell()
            table.Text = "Actual rate(/Hr)"
            table.HorizontalAlign = HorizontalAlign.Center
            table.RowSpan = 2
            table.Wrap = True
            GridRow1.Cells.Add(table)

            table = New TableCell()
            table.Text = "Diff (/Hr)"
            table.HorizontalAlign = HorizontalAlign.Center
            table.RowSpan = 2
            table.Wrap = True
            GridRow1.Cells.Add(table)

            table = New TableCell()
            table.Text = "Berth"
            table.HorizontalAlign = HorizontalAlign.Center
            table.RowSpan = 2
            table.Wrap = True
            GridRow1.Cells.Add(table)

            table = New TableCell()
            table.Text = "Depart"
            table.HorizontalAlign = HorizontalAlign.Center
            table.RowSpan = 2
            table.Wrap = True
            GridRow1.Cells.Add(table)

            table = New TableCell()
            table.Text = "STD. (Hr)"
            table.HorizontalAlign = HorizontalAlign.Center
            table.RowSpan = 2
            table.Wrap = True
            GridRow1.Cells.Add(table)

            table = New TableCell()
            table.Text = "Total (Hr)"
            table.HorizontalAlign = HorizontalAlign.Center
            table.RowSpan = 2
            table.Wrap = True
            GridRow1.Cells.Add(table)

            table = New TableCell()
            table.Text = "Diff (Hr)"
            table.HorizontalAlign = HorizontalAlign.Center
            table.RowSpan = 2
            table.Wrap = True
            GridRow1.Cells.Add(table)

            table = New TableCell()
            table.Text = "Demurrage/(Delay)"
            table.HorizontalAlign = HorizontalAlign.Center
            table.RowSpan = 2
            table.Wrap = True
            GridRow1.Cells.Add(table)

            Grid.Controls(0).Controls.AddAt(1, GridRow1)

            table = New TableCell()
            table.Text = "B/L(1)"
            table.HorizontalAlign = HorizontalAlign.Center
            table.Wrap = True
            GridRow.Cells.Add(table)

            table = New TableCell()
            table.Text = "Ship(2)"
            table.HorizontalAlign = HorizontalAlign.Center
            table.Wrap = True
            GridRow.Cells.Add(table)

            table = New TableCell()
            table.Text = "1&3"
            table.HorizontalAlign = HorizontalAlign.Center
            table.Wrap = True
            GridRow.Cells.Add(table)

            table = New TableCell()
            table.Text = "%"
            table.HorizontalAlign = HorizontalAlign.Center
            table.Wrap = True
            GridRow.Cells.Add(table)

            table = New TableCell()
            table.Text = "2&3"
            table.HorizontalAlign = HorizontalAlign.Center
            table.Wrap = True
            GridRow.Cells.Add(table)

            table = New TableCell()
            table.Text = "%"
            table.HorizontalAlign = HorizontalAlign.Center
            table.Wrap = True
            GridRow.Cells.Add(table)

            Grid.Controls(0).Controls.AddAt(2, GridRow)
        End If
    End Sub

    Private Sub btnSearch_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSearch.Click
        If (txtDate.Text.Trim() = "") Or (txtTIme.Text.Trim = "") Then
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", " alert('กรุณาทำการระบุวันที่และเวลา !!');", True)
            Exit Sub
        End If
        lbDateSHow.Text = "DATE OF : " & CDate(txtDate.Text).ToString("dd-MMM-yy") & " (" & txtTIme.Text & " - CYCLE)"
        ViewState("C_") = "1"
        ViewState("CELL") = ""
        gridItem.DataSource = LoadValue(txtDate.Text, "L")
        gridItem.DataBind()
        ViewState("CELL") = ""
        gridDischarge.DataSource = LoadValue(txtDate.Text, "B")
        gridDischarge.DataBind()
        Dim str As String = ""
        For i As Integer = 1 To CInt(ViewState("C_")) - 1
            str &= i.ToString() & ".)" & vbCrLf
        Next
        txtTotal.Text = str
    End Sub

    Private Function GetNameMonth(ByVal Month As String) As String
        Try
            Select Case (CInt(Month).ToString("00"))
                Case "01"
                    Return "Jan"
                Case "02"
                    Return "Feb"
                Case "03"
                    Return "Mar"
                Case "04"
                    Return "Apr"
                Case "05"
                    Return "May"
                Case "06"
                    Return "Jun"
                Case "07"
                    Return "Jul"
                Case "08"
                    Return "Aug"
                Case "09"
                    Return "Sep"
                Case "10"
                    Return "Oct"
                Case "11"
                    Return "Nov"
                Case "12"
                    Return "Dec"
                Case Else
                    Return ""
            End Select
        Catch ex As Exception
            Return Month
        End Try

    End Function

    Private Sub btnPrint_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPrint.Click
        Dim service As New _ReportingService.Report

        Dim optWindow As String = "left=0,top=0,location=no,menubar=no,resizable=yes,scrollbars=yes,status=yes,toolbar=no"
        Dim Orientation As Boolean = True
        Dim ReportName As String = String.Empty
        Dim reportDataSource As String = String.Empty
        Dim paraName As String = String.Empty
        Dim paraValue As String = String.Empty
        Dim reportType As String = String.Empty
        Dim urlDisplay As String = String.Empty
        Dim dsReport As New DataSet
        dsReport.Tables.Add(Get_Value())
        dsReport.AcceptChanges()
        If (dsReport.Tables(0).Rows.Count > 0) Then
            ReportName = "Dairy_Report.rdlc"
            ' dsReport = New DataSet
            reportDataSource = "Daily_Value"
            paraName = "D_TIME;Working_Control;PTOL;Main;Aver;Sea;Emer;Oil;DateTimeNow"
            paraValue = lbDateSHow.Text & ";" & txtTotal.Text & ";" & txtPTOL.Text & ";" & txtMain.Text & ";" & txtAver.Text & ";" & ddlChoice.SelectedItem.Text & ";" & txtEmer.Text & ";" & txtOil.Text & ";" & DateTime.Now.ToString()
            Orientation = True
            reportType = "excel"

            urlDisplay = service.CreateReporting(dsReport, reportDataSource, reportType, ReportName, paraName, paraValue, Orientation, "")

            Dim oldFile As String = String.Empty
            oldFile = Server.MapPath(urlDisplay)
            Dim fileInfo As FileInfo = New FileInfo(oldFile)
            Dim reportPath As String = "../WriteReport/"

            Dim newFileName As String = DateTime.Now.ToString("_yyMMdd_HHmmss", New CultureInfo("en-US"))
            newFileName = "Daily_Report" & newFileName

            If fileInfo.Exists Then
                fileInfo.MoveTo(fileInfo.DirectoryName & "/" & newFileName & fileInfo.Extension)
                urlDisplay = reportPath & newFileName & fileInfo.Extension
            End If

            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "window.open('" + urlDisplay + "','_blank','" + optWindow + "');", True)
        Else
            ScriptManager.RegisterClientScriptBlock(Me.Page, GetType(String), "myscript", "alert('ไม่พบข้อมูลการใช้งานเรือทัก');", True)
            Exit Sub
        End If

    End Sub

    Private Function Get_Value() As DataTable
        Dim dt As DataTable = CreateDS()
        For i As Integer = 0 To gridItem.Rows.Count - 1
            Dim dr As DataRow = dt.NewRow()
            dr.Item("Head") = "L"
            dr.Item("Wharf") = gridItem.Rows(i).Cells(0).Text
            dr.Item("Ship") = gridItem.Rows(i).Cells(1).Text.Replace("&nbsp;", "")
            dr.Item("Product") = gridItem.Rows(i).Cells(2).Text.Replace("&nbsp;", "")
            dr.Item("Shore") = gridItem.Rows(i).Cells(3).Text.Replace("&nbsp;", "")
            dr.Item("Third") = gridItem.Rows(i).Cells(4).Text.Replace("&nbsp;", "")
            dr.Item("Unit") = gridItem.Rows(i).Cells(5).Text.Replace("&nbsp;", "")
            dr.Item("BL") = gridItem.Rows(i).Cells(6).Text.Replace("&nbsp;", "")
            dr.Item("SL") = gridItem.Rows(i).Cells(7).Text.Replace("&nbsp;", "")
            dr.Item("Diff") = gridItem.Rows(i).Cells(8).Text.Replace("&nbsp;", "")
            dr.Item("PersendDiff") = gridItem.Rows(i).Cells(9).Text.Replace("&nbsp;", "")
            dr.Item("E_API") = gridItem.Rows(i).Cells(10).Text.Replace("&nbsp;", "")
            dr.Item("E_VIS") = gridItem.Rows(i).Cells(11).Text.Replace("&nbsp;", "")
            dr.Item("S_API") = gridItem.Rows(i).Cells(12).Text.Replace("&nbsp;", "")
            dr.Item("S_VIS") = gridItem.Rows(i).Cells(13).Text.Replace("&nbsp;", "")

            Dim txtAcceptance As TextBox = CType(gridItem.Rows(i).FindControl("txtAcceptance"), TextBox)
            Dim txtDe As TextBox = CType(gridItem.Rows(i).FindControl("txtDe"), TextBox)
            Dim txtPig As TextBox = CType(gridItem.Rows(i).FindControl("txtPig"), TextBox)
            Dim txtFW As TextBox = CType(gridItem.Rows(i).FindControl("txtFW"), TextBox)
            Dim txtTruck As TextBox = CType(gridItem.Rows(i).FindControl("txtTruck"), TextBox)

            dr.Item("Accept") = txtAcceptance.Text
            dr.Item("Standard") = gridItem.Rows(i).Cells(15).Text.Replace("&nbsp;", "")
            dr.Item("UnitFlow") = gridItem.Rows(i).Cells(16).Text.Replace("&nbsp;", "")
            dr.Item("Actual") = gridItem.Rows(i).Cells(17).Text.Replace("&nbsp;", "")
            dr.Item("DiffFlow") = gridItem.Rows(i).Cells(18).Text.Replace("&nbsp;", "")
            dr.Item("Berth") = gridItem.Rows(i).Cells(19).Text.Replace("&nbsp;", "")
            dr.Item("Depart") = gridItem.Rows(i).Cells(20).Text.Replace("&nbsp;", "")
            dr.Item("STD") = gridItem.Rows(i).Cells(21).Text.Replace("&nbsp;", "")
            dr.Item("Total") = gridItem.Rows(i).Cells(22).Text.Replace("&nbsp;", "")
            dr.Item("DiffTime") = gridItem.Rows(i).Cells(23).Text.Replace("&nbsp;", "")
            dr.Item("Demurrage") = txtDe.Text
            dr.Item("Pig") = txtPig.Text
            dr.Item("FW") = txtFW.Text
            dr.Item("Bunker") = txtTruck.Text
            dr.Item("Status") = gridItem.Rows(i).Cells(28).Text.Replace("&nbsp;", "")

            If (gridItem.DataKeys(i).Values("Booking_no") & "" <> "") And (gridItem.DataKeys(i).Values("Product_no") & "" <> "") Then
                InsertData(gridItem.DataKeys(i).Values("Booking_no") & "", gridItem.DataKeys(i).Values("Product_no") & "", txtAcceptance.Text, txtDe.Text, txtPig.Text, txtFW.Text, txtTruck.Text)
            End If


            dt.Rows.Add(dr)
            dt.AcceptChanges()


        Next

        For i As Integer = 0 To gridDischarge.Rows.Count - 1
            Dim dr As DataRow = dt.NewRow()
            dr.Item("Head") = "D"
            dr.Item("Wharf") = gridDischarge.Rows(i).Cells(0).Text
            dr.Item("Ship") = gridDischarge.Rows(i).Cells(1).Text.Replace("&nbsp;", "")
            dr.Item("Product") = gridDischarge.Rows(i).Cells(2).Text.Replace("&nbsp;", "")
            dr.Item("Shore") = gridDischarge.Rows(i).Cells(3).Text.Replace("&nbsp;", "")
            dr.Item("Third") = gridDischarge.Rows(i).Cells(4).Text.Replace("&nbsp;", "")
            dr.Item("Unit") = gridDischarge.Rows(i).Cells(5).Text.Replace("&nbsp;", "")
            dr.Item("BL") = gridDischarge.Rows(i).Cells(6).Text.Replace("&nbsp;", "")
            dr.Item("SL") = gridDischarge.Rows(i).Cells(7).Text.Replace("&nbsp;", "")
            dr.Item("Diff") = gridDischarge.Rows(i).Cells(8).Text.Replace("&nbsp;", "")
            dr.Item("E_API") = gridDischarge.Rows(i).Cells(9).Text.Replace("&nbsp;", "")
            dr.Item("E_VIS") = gridDischarge.Rows(i).Cells(10).Text.Replace("&nbsp;", "")
            dr.Item("S_API") = gridDischarge.Rows(i).Cells(11).Text.Replace("&nbsp;", "")
            dr.Item("S_VIS") = gridDischarge.Rows(i).Cells(12).Text.Replace("&nbsp;", "")

            Dim txtCheck As TextBox = CType(gridDischarge.Rows(i).FindControl("txtCheck"), TextBox)
            Dim txtDe As TextBox = CType(gridDischarge.Rows(i).FindControl("txtDe"), TextBox)
            Dim txtPig As TextBox = CType(gridDischarge.Rows(i).FindControl("txtPig"), TextBox)
            Dim txtFW As TextBox = CType(gridDischarge.Rows(i).FindControl("txtFW"), TextBox)
            Dim txtTruck As TextBox = CType(gridDischarge.Rows(i).FindControl("txtTruck"), TextBox)

            dr.Item("Accept") = txtCheck.Text
            dr.Item("Standard") = gridDischarge.Rows(i).Cells(14).Text.Replace("&nbsp;", "")
            dr.Item("UnitFlow") = gridDischarge.Rows(i).Cells(15).Text.Replace("&nbsp;", "")
            dr.Item("Actual") = gridDischarge.Rows(i).Cells(16).Text.Replace("&nbsp;", "")
            dr.Item("DiffFlow") = gridDischarge.Rows(i).Cells(17).Text.Replace("&nbsp;", "")
            dr.Item("Berth") = gridDischarge.Rows(i).Cells(18).Text.Replace("&nbsp;", "")
            dr.Item("Depart") = gridDischarge.Rows(i).Cells(19).Text.Replace("&nbsp;", "")
            dr.Item("STD") = gridDischarge.Rows(i).Cells(20).Text.Replace("&nbsp;", "")
            dr.Item("Total") = gridDischarge.Rows(i).Cells(21).Text.Replace("&nbsp;", "")
            dr.Item("DiffTime") = gridDischarge.Rows(i).Cells(22).Text.Replace("&nbsp;", "")
            dr.Item("Demurrage") = txtDe.Text
            dr.Item("Pig") = txtPig.Text
            dr.Item("FW") = txtFW.Text
            dr.Item("Bunker") = txtTruck.Text
            dr.Item("Status") = gridDischarge.Rows(i).Cells(27).Text.Replace("&nbsp;", "")

            If (gridItem.DataKeys(i).Values("Booking_no") & "" <> "") And (gridItem.DataKeys(i).Values("Product_no") & "" <> "") Then
                InsertData(gridItem.DataKeys(i).Values("Booking_no") & "", gridItem.DataKeys(i).Values("Booking_no") & "", txtCheck.Text, txtDe.Text, txtPig.Text, txtFW.Text, txtTruck.Text)
            End If

            dt.Rows.Add(dr)
            dt.AcceptChanges()
        Next

        Return dt
    End Function

    Private Function InsertData(ByVal Booking As String, ByVal Product_no As String, ByVal Acceptance As String, ByVal Demurrage As String, ByVal Pig As String, ByVal FW As String, ByVal Truck As String) As Boolean
        Dim conn As New System.Data.SqlClient.SqlConnection(connString)
        Dim clsDB As New ClassDatabase
        Dim clsFc As New ClassFunction
        Dim condition As Boolean = True
        Dim tran As System.Data.SqlClient.SqlTransaction = Nothing
        Application.Lock()
        conn.Open()
        tran = clsDB.BeginTran(conn)
        clsFc.DeleteDaily_LCT(Booking, Product_no, conn, tran)
        If (clsFc.InsertDaily_LCT(Booking, Product_no, Acceptance, Demurrage, Pig, FW, Truck, conn, tran) < 1) Then
            condition = False
        End If
        If (condition) Then
            clsDB.CommitTran(tran)
            conn.Close()
            Application.UnLock()
            conn.Dispose()
            Return True
        Else
            clsDB.RollBackTran(tran)
            conn.Close()
            Application.UnLock()
            conn.Dispose()
            Return False
        End If
    End Function

    Private Sub gridItem_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gridItem.RowDataBound
        If (e.Row.RowType = DataControlRowType.DataRow) Then
            Try
                If (e.Row.Cells(0).Text = ViewState("CELL") & "") Then
                    e.Row.Cells(0).Text = ""
                Else
                    ViewState("CELL") = e.Row.Cells(0).Text
                End If
            Catch ex As Exception

            End Try
            Dim dt As DataTable = clsget.Get_Daily_LCT(gridItem.DataKeys(e.Row.RowIndex).Values("Booking_no") & "", gridItem.DataKeys(e.Row.RowIndex).Values("Product_no") & "")
            If (dt.Rows.Count > 0) Then
                Dim txtAcceptance As TextBox = CType(e.Row.FindControl("txtAcceptance"), TextBox)
                Dim txtDe As TextBox = CType(e.Row.FindControl("txtDe"), TextBox)
                Dim txtPig As TextBox = CType(e.Row.FindControl("txtPig"), TextBox)
                Dim txtFW As TextBox = CType(e.Row.FindControl("txtFW"), TextBox)
                Dim txtTruck As TextBox = CType(e.Row.FindControl("txtTruck"), TextBox)

                txtAcceptance.Text = dt.Rows(0).Item("Acceptance") & ""
                txtDe.Text = dt.Rows(0).Item("Demurrage") & ""
                txtPig.Text = dt.Rows(0).Item("Pig") & ""
                txtFW.Text = dt.Rows(0).Item("FW") & ""
                txtTruck.Text = dt.Rows(0).Item("Truck") & ""
            End If

        End If
    End Sub

    Private Sub gridDischarge_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gridDischarge.RowDataBound
        If (e.Row.RowType = DataControlRowType.DataRow) Then
            Try
                If (e.Row.Cells(0).Text = ViewState("CELL") & "") Then
                    e.Row.Cells(0).Text = ""
                Else
                    ViewState("CELL") = e.Row.Cells(0).Text
                End If
            Catch ex As Exception

            End Try
            Dim dt As DataTable = clsget.Get_Daily_LCT(gridDischarge.DataKeys(e.Row.RowIndex).Values("Booking_no") & "", gridDischarge.DataKeys(e.Row.RowIndex).Values("Product_no") & "")
            If (dt.Rows.Count > 0) Then
                Dim txtCheck As TextBox = CType(e.Row.FindControl("txtCheck"), TextBox)
                Dim txtDe As TextBox = CType(e.Row.FindControl("txtDe"), TextBox)
                Dim txtPig As TextBox = CType(e.Row.FindControl("txtPig"), TextBox)
                Dim txtFW As TextBox = CType(e.Row.FindControl("txtFW"), TextBox)
                Dim txtTruck As TextBox = CType(e.Row.FindControl("txtTruck"), TextBox)

                txtCheck.Text = dt.Rows(0).Item("Acceptance") & ""
                txtDe.Text = dt.Rows(0).Item("Demurrage") & ""
                txtPig.Text = dt.Rows(0).Item("Pig") & ""
                txtFW.Text = dt.Rows(0).Item("FW") & ""
                txtTruck.Text = dt.Rows(0).Item("Truck") & ""
            End If
        End If
    End Sub
End Class